/* 
 * File:   HttpServer.cpp
 * Author: ever
 * 
 * Created on 2016年4月29日, 上午9:30
 */

#include "HttpServer.h"
#include "Udp.h"
#include "KKSManger.h"


using namespace std;
#define DAY_SECONDS (24 * 60 * 60)
#define BEIJING_TIME_INV (8 * 60 * 60)

void EdsRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) {
    std::ostream & ostr = response.send();
    const std::string& uri = request.getURI();
    if (uri.find("/Api/eds/tgn/") != std::string::npos) {
        try {
            std::vector<std::string> wizids;
            tidp::TL_Common::split(wizids, uri.substr(sizeof ("/Api/eds/tgn/") - 1), ",");
            KKSManger::getInstance()->getMultiKKSFromCache(ostr, wizids);

        } catch (const exception& e) {
            ostr << "{\"ret\":-1,\"ver\":\"2.0.0\",\"mod\":\"0.0.1\",\"msg\"=\"" << e.what() << "\"}\n";
        } catch (...) {
            ostr << "{\"ret\":-1,\"ver\":\"2.0.0\",\"mod\":\"0.0.1\",\"msg\"=\"unknow exception.\"}\n";
        }
    } else if (uri.find("/Api/eds/history/") != std::string::npos) {
        string subquery = uri.substr(sizeof ("/Api/eds/history/") - 1);
        //tgn/$tgn/$stime/$etime
        //tgn配置文件的迁移,会对该方案产生影响
        std::vector<std::string> tmp;
        tidp::TL_Common::split(tmp, subquery, "/");
        if (tmp.size() < 2) {
            ostr << "{ret:-1,retmsg:\"param not enough\"}";
        } else {
            time_t now = time(NULL);
            string tgn = tmp[0];
            time_t stime = 0;
            if (tmp[1].size() > 0) stime = atoll(tmp[1].c_str());
            time_t etime = 0;
            if (tmp.size() > 2 && tmp[2].size() > 0) etime = atoll(tmp[2].c_str());
            if (etime <= 0 || etime > now) etime = now;
            //对于濮阳与周口，udp传输过来的kks为数字编号，这里调整下逻辑
            if (tgn.size() < 1 || stime > etime || stime <= 0 || etime <= 0) {
                ostr << "{ret:-1,retmsg:\"param  error:[" << tgn << "," << stime << "," << etime << "]\"}";
            } else if (etime - stime > DAY_SECONDS ||
                    (stime + BEIJING_TIME_INV) / DAY_SECONDS != (etime + BEIJING_TIME_INV) / DAY_SECONDS
                    ) {
                ostr << "{ret:-1,retmsg:\"stime etime not in same day.\"}";
            } else {
                u32_t kks_id = strtoul(tgn.c_str(), NULL, 0);
                KKSManger::getInstance()->history(ostr, tgn,kks_id, stime, etime);
                /*
                map<string, pair<EdsPointThread*, int> >::const_iterator it = _tgn_thread_liveid.find(tgn);
                if (it == _tgn_thread_liveid.end()) {
                    ostr << "{ret:-1,retmsg:\"tgn:" << tgn << " is not available.\"}";
                } else {
                    EdsPointThread* eptr = it->second.first;
                    eptr->history(ostr, tgn, stime, etime);
                }*/
            }
        }

    } else {
        ostr << "{\"ret\":-1,\"msg\":\"unknow api.\"}\n";
    }
    ///Api/eds/wizids/PR.AUX.AI.Y0BHE02CE101,PR.AUX.AI.Y0GCB12DP101
}

EdsHttpServer::EdsHttpServer() {
}

EdsHttpServer::EdsHttpServer(const EdsHttpServer & orig) {
}

EdsHttpServer::~EdsHttpServer() {
}

void EdsHttpServer::initialize(Application & self) {
    loadConfiguration(); // load default configuration files, if present
    ServerApplication::initialize(self);
}

void EdsHttpServer::uninitialize() {
    ServerApplication::uninitialize();
}

int EdsHttpServer::main(const std::vector<std::string>& arg) {
    ServerSocket svs(_port);
    HTTPServerParams *pParams = new HTTPServerParams;
    pParams->setMaxQueued(100);
    pParams->setMaxThreads(16);
    HTTPServer svr(new EdsRequestHandlerFactory(), svs, pParams);
    cout << "svr.start ..." << endl;
    svr.start();
    waitForTerminationRequest();
    cout << "stop ..." << endl;
    svr.stop();
    return Application::EXIT_OK;
}
